﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DEMO3.BUS;
using DEMO3.DTO;

namespace DEMO3.GUI
{
    public partial class PhongChieu : UserControl
    {
        public PhongChieu()
        {
            InitializeComponent();
        }

        private void PhongChieu_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            PhongChieuBUS test = new PhongChieuBUS();
            gc.DataSource = test.Get();
            cbxDKKT.SelectedIndex = 0;

            RapChieuBUS rc = new RapChieuBUS();
            cbxMaRap.DataSource = rc.Get();
            cbxMaRap.ValueMember = "MaRap";
            cbxMaRap.DisplayMember = "TenRap";
        }

        private void gv_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if(gv.FocusedRowHandle!=gv.RowCount && gv.RowCount!=0)
            {
                txtSoGhe.Text = gv.GetRowCellValue(gv.FocusedRowHandle, "SoGheToiDa").ToString();
                txtDayToiDa.Text = gv.GetRowCellValue(gv.FocusedRowHandle, "DayGheToiDa").ToString();
                txtMoTa.Text = gv.GetRowCellValue(gv.FocusedRowHandle, "MoTa").ToString();
                cbxDKKT.SelectedItem = gv.GetRowCellValue(gv.FocusedRowHandle, "DieuKienKiThuat").ToString();
                cbxMaRap.SelectedValue = gv.GetRowCellValue(gv.FocusedRowHandle, "MaRap").ToString();
                cbTrangThai.Checked = bool.Parse(gv.GetRowCellValue(gv.FocusedRowHandle, "TrangThai").ToString());
            }
        }

        private void btThem_Click(object sender, EventArgs e)
        {
            PhongChieuDTO dto = new PhongChieuDTO();
            dto.SoGhe = int.Parse(txtSoGhe.Text);
            dto.SoDay = int.Parse(txtDayToiDa.Text);
            dto.MoTa = txtMoTa.Text;
            dto.DKKiThuat = cbxDKKT.SelectedText.ToString();
            dto.MaRap = int.Parse(cbxMaRap.SelectedValue.ToString());
            dto.TrangThai = cbTrangThai.Checked;

            PhongChieuBUS bus = new PhongChieuBUS();
            bus.Add(dto);

            LoadData();
        }

        private void btXoa_Click(object sender, EventArgs e)
        {
            PhongChieuDTO dto = new PhongChieuDTO();
            dto.MaPhong = int.Parse(gv.GetRowCellValue(gv.FocusedRowHandle, "MaPhong").ToString());

            PhongChieuBUS bus = new PhongChieuBUS();
            bus.Delete(dto);

            LoadData();
        }

        private void btSua_Click(object sender, EventArgs e)
        {
            PhongChieuDTO dto = new PhongChieuDTO();
            dto.MaPhong = int.Parse(gv.GetRowCellValue(gv.FocusedRowHandle, "MaPhong").ToString());
            dto.SoGhe = int.Parse(txtSoGhe.Text);
            dto.SoDay = int.Parse(txtDayToiDa.Text);
            dto.MoTa = txtMoTa.Text;
            dto.DKKiThuat = cbxDKKT.SelectedItem.ToString();
            dto.MaRap = int.Parse(cbxMaRap.SelectedValue.ToString());
            dto.TrangThai = cbTrangThai.Checked;

            PhongChieuBUS bus = new PhongChieuBUS();
            bus.Update(dto);

            LoadData();
        }
    }
}
